/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package call_center;
import java.sql.*;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author escuela chiloe 06
 */
public class Conectate {
     Connection call_center;
     Statement stm;
     PreparedStatement ps;
     ResultSet rs;
     PreparedStatement pst;
    
     
    static String bd = "Call_center";//nombre de la base de datos que esta en mysql
    static String login = "root";// usuario
    static String password = "";// contraseña
    static String url = "jdbc:mysql://localhost/"+bd;
   
   
   public Conectate() throws ClassNotFoundException, SQLException  {
      try{
         Class.forName("com.mysql.jdbc.Driver");
         String sss = "jdbc:mysql://localhost/call_center";
         call_center=DriverManager.getConnection("jdbc:mysql://localhost/call_center","root","");
         stm = call_center.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
      }catch(   ClassNotFoundException | SQLException ex){
        throw ex;
      }
   }

    
    /**Permite retornar la conexió
     * @return n*/
   public Connection getConnection(){
     return call_center;
   }
   public ResultSet consulta(String sql) throws SQLException {
       
      try{
          rs = stm.executeQuery(sql);
      }catch (SQLException ex){
          throw ex;
          
      }
         return rs;
       
   }
           
   public DefaultTableModel restonarDatos(String sentenciaSQL){
        
       DefaultTableModel defaul = new DefaultTableModel();
       {try{
           ResultSet rdRegistro_Solicitudes = consulta(sentenciaSQL); 
           ResultSetMetaData mdatos=rdRegistro_Solicitudes.getMetaData();
           int numeroColumnas=mdatos.getColumnCount();
           Object[] etiqueta=new Object [numeroColumnas];
           for(int i=0; i < numeroColumnas;i++){
               etiqueta[i]=mdatos.getColumnLabel(i+1);
           }
           defaul.setColumnIdentifiers(etiqueta);
           while(rdRegistro_Solicitudes.next()){
           Object[] datosfila = new Object[defaul.getColumnCount()];
       
           for (int i=0; i < defaul.getColumnCount();i++){
               datosfila [i]=rdRegistro_Solicitudes.getObject(i+1);
           }
           defaul.addRow(datosfila);
           }
       }catch (SQLException e){
           System.out.println(e.getMessage());
       }
       return defaul;
           
       }
   }

    private ResultSet call_center(String sentenciaSQL) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
   
}
